// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NotifyPropertyChangedTester.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Hisaab.Client.Test.NotifyPropertyChangedTester" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Desktop.Tests
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Defines the <seealso cref="NotifyPropertyChangedTester"/> type.
    /// </summary>
    public class NotifyPropertyChangedTester
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NotifyPropertyChangedTester"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        public NotifyPropertyChangedTester(INotifyPropertyChanged viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel", "Argument cannot be null.");
            }

            Changes = new List<string>();

            viewModel.PropertyChanged += ViewModelPropertyChanged;
        }

        /// <summary>
        /// Gets the changes.
        /// </summary>
        public List<string> Changes { get; private set; }

        /// <summary>
        /// Asserts the change.
        /// </summary>
        /// <param name="changeIndex">Index of the change.</param>
        /// <param name="expectedPropertyName">Expected name of the property.</param>
        public void AssertChange(int changeIndex, string expectedPropertyName)
        {
            Assert.IsNotNull(Changes, "Changes collection was null.");

            Assert.IsTrue(
                changeIndex < Changes.Count,
                "Changes collection contains '{0}' items and does not have an element at index '{1}'.",
                Changes.Count,
                changeIndex);

            Assert.AreEqual(
                expectedPropertyName,
                Changes[changeIndex],
                "Change at index '{0}' is '{1}' and is not equal to '{2}'.",
                changeIndex,
                Changes[changeIndex],
                expectedPropertyName);
        }

        /// <summary>
        /// Handles the PropertyChanged event of the viewModel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void ViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Changes.Add(e.PropertyName);
        }
    }
}